package com.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.Data;
import lombok.val;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users/{username}")
    public User getByUsername(@PathVariable String username) {
        val userMap = createUserMap();
        return userMap.get(username);
    }

    private Map<String, User> createUserMap() {
        User u1 = User.builder().id(1).username("xiaoming").build();
        User u2 = User.builder().id(2).username("xiaohong").build();
        Map<String,User> userMap = new HashMap<>();
        userMap.put(u1.getUsername(), u1);
        userMap.put(u2.getUsername(), u1);
        return userMap;
    }

    @Builder
    @Data
    public static class User{
        private int id;
        private String username;
        public String toString(){
            String s = "User{id:"+id+",username:"+username+"}";
            System.out.println(s);
            return s;
        }
    }

}
